package com.svmc.app.task.activity;

import java.util.ArrayList;

import luyenlv.pro.screen.base.BaseActivity;
import android.content.Context;
import android.content.SharedPreferences;
import android.os.Bundle;
import android.preference.PreferenceManager;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.TextView;
import android.widget.Toast;
import android.widget.ToggleButton;

import com.svmc.app.task.utils.CommonDefine;
import com.svmc.app.task.view.CustomDialog;

public class SettingActivity extends BaseActivity implements OnClickListener,CommonDefine{
	public static TextView deleteschedule;
	public static TextView alarmFrequency;
	public static TextView dateformat;
	public static TextView timeformat;
	ToggleButton shareOnline,synchronize;
	CustomDialog mydialog;
	@Override
	protected void onCreate(Bundle savedInstanceState) {
		// TODO Auto-generated method stub
		super.onCreate(savedInstanceState);
		setContentView(getLayout(R.array.setting_layout));		
		mydialog = new CustomDialog(getDialogContext());
		findViewById(R.id.setting_img_back).setOnClickListener(this);
		findViewById(R.id.setting_img_done).setOnClickListener(this);
		deleteschedule =(TextView)findViewById(R.id.setting_txt_delete_schedule);
		deleteschedule.setOnClickListener(this);
		alarmFrequency=(TextView)findViewById(R.id.setting_txt_alarm_frequency);
		alarmFrequency.setOnClickListener(this);
		dateformat=(TextView)findViewById(R.id.setting_txt_dateformat);
		dateformat.setOnClickListener(this);
		timeformat =(TextView)findViewById(R.id.setting_txt_timeformat);
		timeformat.setOnClickListener(this);
		deleteschedule.setText(mydialog.getPreferences(DELETE_SCHEDULE_TYPE_SETTING));
		alarmFrequency.setText(mydialog.getPreferences(ALARM_FREQUENCY_TYPE_SETTING));
		dateformat.setText(mydialog.getPreferences(DATE_FORMAT_TYPE));
		timeformat.setText(mydialog.getPreferences(TIME_FORMAT_TYPE));
		shareOnline =(ToggleButton)findViewById(R.id.setting_toggleButton_shareonline);
		shareOnline.setOnClickListener(this);
		shareOnline.setChecked(getbooleanPreferences(SHARE_ONLINE_TYPE));
		synchronize =(ToggleButton)findViewById(R.id.setting_toggleButton_synchronize);
		synchronize.setOnClickListener(this);
		synchronize.setChecked(getbooleanPreferences(SYNCHRONIZE_TYPE));
	}

	@Override
	public void clearMemory() {
	
		
	}

	@Override
	public void onClick(View v) {
		
		switch (v.getId()) {
		case R.id.setting_img_back:
			finish();
			break;
		case R.id.setting_img_done:
			showToast("Setting is saved");			
			break;
		case R.id.setting_txt_delete_schedule:
			mydialog.setDialogType(DIALOG_NOTIFY,this.getString(R.string.notify_upgrade_version));
			mydialog.show();
			/*
			 ArrayList<String> dataSchudeleDelete = new ArrayList<String>();
			 dataSchudeleDelete.add(VALUE_SETTING_SCHEDULE_DELETE_OFF);
			 dataSchudeleDelete.add(VALUE_SETTING_SCHEDULE_DELETE_ONE_DAY);
			 dataSchudeleDelete.add(VALUE_SETTING_SCHEDULE_DELETE_ONE_MONTH);
			 dataSchudeleDelete.add(VALUE_SETTING_SCHEDULE_DELETE_ONE_YEAR);
			mydialog.setDialogType(DIALOG_CHOOSE_SETTING,getString(R.string.title_dialog_choose_setting_delete_scheduele), dataSchudeleDelete);
			mydialog.show();
			*/
			break;
		case R.id.setting_txt_alarm_frequency:
			mydialog.setDialogType(DIALOG_NOTIFY,this.getString(R.string.notify_upgrade_version));
			mydialog.show();
			/*
			 ArrayList<String> dataAlarmFrequency = new ArrayList<String>();
			 dataAlarmFrequency.add(VALUE_SETTING_ALARM_FREQUENCY_OFF);
			 dataAlarmFrequency.add(VALUE_SETTING_ALARM_FREQUENCY_EVERYDAY);
			 mydialog.setDialogType(DIALOG_CHOOSE_SETTING,getString(R.string.title_dialog_choose_setting_alarm_frequency), dataAlarmFrequency);
			 mydialog.show();
			 */
			break;
		case R.id.setting_txt_dateformat:
			ArrayList<String> dateformat = new ArrayList<String>();
			dateformat.add(VALUE_SETTING_DATE_FORMAT_1);
			dateformat.add(VALUE_SETTING_DATE_FORMAT_2);
			mydialog.setDialogType(DIALOG_CHOOSE_SETTING, getString(R.string.title_dialog_choose_setting_date_format), dateformat);
			mydialog.show();
			break;
		case R.id.setting_txt_timeformat:
			ArrayList<String> timeformat = new ArrayList<String>();
			timeformat.add(VALUE_SETTING_TIME_FORMAT_1);
			timeformat.add(VALUE_SETTING_TIME_FORMAT_2);
			mydialog.setDialogType(DIALOG_CHOOSE_SETTING, getString(R.string.title_dialog_choose_setting_time_format), timeformat);
			mydialog.show();
			break;
		case R.id.setting_toggleButton_shareonline:
				if (shareOnline.isChecked()){
					savebooleanPreferences(SHARE_ONLINE_TYPE, true);
				}else {
					savebooleanPreferences(SHARE_ONLINE_TYPE, false);
				}
			
			break;
		case R.id.setting_toggleButton_synchronize:
			
//			if (synchronize.isChecked()){
//				savebooleanPreferences(SYNCHRONIZE_TYPE, true);
//			}else {
//				savebooleanPreferences(SYNCHRONIZE_TYPE, false);
//			}
			synchronize.setChecked(false);
			mydialog.setDialogType(DIALOG_NOTIFY,this.getString(R.string.notify_upgrade_version));
			mydialog.show();
			break;
		default:
			break;
		}
		
	}
	public void showToast(String info){
		Toast.makeText(this, info, Toast.LENGTH_LONG).show();
	}
	/**
	 *  Get context in groupActivity.Use for show dialog
	 * @return : context
	 */
	private Context getDialogContext() {
	    Context mcontext;
	    if (getParent() != null) mcontext = getParent();
	    else mcontext = this;
	    return mcontext;
	}
	public boolean getbooleanPreferences(String type){
		boolean temp;
		SharedPreferences dataSetting= PreferenceManager.getDefaultSharedPreferences(this);
		temp= dataSetting.getBoolean(type, false);
		return temp;
	}
	public void savebooleanPreferences (String type,boolean value){
		SharedPreferences dataSetting= PreferenceManager.getDefaultSharedPreferences(this);
		SharedPreferences.Editor editor = dataSetting.edit();
		editor.putBoolean(type, value);
		editor.commit();
		
	}

}
